package com.maoye.ossp.bpm.bpmweb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.maoye.ossp.bpmagent.context.BpmAgentConfiguration;
import com.maoye.ossp.bpmagent.model.TaskInstance;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;

@Controller
@RequestMapping("bpm/bpmweb/")
public class BpmOsspController extends BaseController{
	
	@Resource(name = "bpmAgentConfiguration")
	private BpmAgentConfiguration bpmAgentConfiguration;
	/**
	 * 审批的任务信息
	 * 		通过流程状态的不同查询不同任务信息
	 * @param status 任务状态
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("approvedTask/{status}")
	public String approvedTask(@PathVariable(value = "status") String status,
				HttpServletRequest request,HttpServletResponse response,Model model){
		// 缺失用户ID（通过会话session获取用户ID）
		Long userId=UserUtils.getUser().getId(); 
		List<TaskInstance> list=new ArrayList<TaskInstance>();
		list=bpmAgentConfiguration.getBpmAgentContext().getTaskEngine("taskEngineImpl")
				.getTaskByUserIdandStatus(userId.toString(), status);
//				.getTaskByUserIdandStatus("1", status);
		 
		model.addAttribute("list",list); 
		
		return resultPage("/bpm/agent/task/taskList", model, request);
	}
	
	/**
	 * 通过任务Id 查询任务信息
	 * @param taskId 任务ID
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("taskView/{taskId}")
	public String taskView(@PathVariable(value="taskId") String taskId,
			HttpServletRequest request,HttpServletResponse response,Model model){
		 TaskInstance taskInstance=new TaskInstance();
//		 taskInstance=bpmAgentConfiguration.getBpmAgentContext().getTaskEngine("taskEngineImpl")
//				 		.getTaskById(taskId); 
//		 model.addAttribute("taskInstance",taskInstance); 
		 return "/bpm/agent/task/taskView";
	}
 
	/**
	 *审批流程信息
	 *	通过流程状态查询不同流程信息
	 * @param status 流程状态
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("approvedProcess/{status}")
	public String approvedProcess(@PathVariable(value = "status") String status,
			HttpServletRequest request,HttpServletResponse response,Model model){
		// 缺失用户ID（通过会话session获取用户ID）
//		Long userId=UserUtils.getUser().getId(); 
//		List<ProcessInstance> list=bpmAgentConfiguration
//						.getBpmAgentContext()
//						.getProcessInstanceEngine("processInstanceEngineImpl")
//						.getOpinionByUserIdAndStatus(userId.toString(), status);
//		model.addAttribute("list",list);
		
		return resultPage("/bpm/agent/process/processList", model, request);  
	}
	/**
	 * 通过实例ID，查询流程实例信息
	 * @param instanceId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("processView/{instanceId}")
	public  String processView(@PathVariable(value="instanceId") String instanceId,
			HttpServletRequest request,HttpServletResponse response,Model model){
//		ProcessInstance processInstance=new ProcessInstance();
//		processInstance=bpmAgentConfiguration
//				.getBpmAgentContext()
//				.getProcessInstanceEngine("processInstanceEngineImpl")
//				.getProcessInstanceById(instanceId);
//		 model.addAttribute("processInstance",processInstance); 
		return "/bpm/agent/process/processView";
	}
}
